<!DOCTYPE html>
<html lang="en">
<head>
    <title>XmppDotNet: A Minimal Working Example (in C# / .NET)</title>
    <link href="style.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/default.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/languages/csharp.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/languages/bash.min.js"></script>
    <script>hljs.highlightAll();</script>
</head>
<body>

<article>

    <header>
        <img src="images/header_logo.gif" alt="Openfire Logo" />
        <h1>XmppDotNet: A Minimal Working Example (in C# / .NET)</h1>
    </header>

    <nav>
        <a href="index.html">&laquo; Back to documentation index</a>
    </nav>

    <section id="intro">

        <h2>Introduction</h2>

        <p>
            This document provides a minimal working example of a client implementation using the XmppDotNet library, making
            it connect to a running Openfire server.
        </p>

        <p>Topics that are covered in this document:</p>

        <nav>
            <ul>
                <li><a href="#background">Background</a>
                <li><a href="#preparations">Preparations</a>
                <li><a href="#code">Code</a>
                <li><a href="#references">Further Reading</a>
            </ul>
        </nav>

    </section>

    <section id="background">

        <h2>Background</h2>

        <p>
            <a href="https://xmppdotnet.org/">XmppDotNet</a> is a cross platform XMPP SDK for the full .NET Framework,
            .NET Core and Mono
        </p>
        <p>
            This guide describes how to use XmppDotNet to connect to Openfire. It provides nothing more than a minimal
            working example, intended as a stepping stone to for client developers that get started with a new project.
        </p>

    </section>

    <section id="preparations">

        <h2>Preparations</h2>

        <p>
            In this example, a client connection will be made against a running Openfire server. For ease of
            configuration, the 'demoboot' setup of Openfire is used.
        </p>
        <p>
            The 'demoboot' setup of Openfire allows one to start a fresh installation of Openfire into a certain
            provisioned state, without running any of the setup steps. When running in 'demoboot' mode:
        </p>
        <ul>
            <li>an administrative account is created using the username 'admin' and password 'admin'</li>
            <li>two users are automatically created: 'jane' and 'john' (both using the value 'secret' as their password)</li>
            <li>the XMPP domain name is configured to be 'example.org' (for ease of use, configure 'example.org' to be an alias of '127.0.0.1' in your hosts file!)</li>
        </ul>
        <p>
            To start Openfire in 'demoboot' mode, you can invoke the Openfire executable using the <code>-demoboot</code>
            argument, as shown below.
        </p>
        <fieldset>
            <legend>Starting Openfire in 'demoboot' mode.</legend>
            <pre><code>$ ./bin/openfire.sh -demoboot</code></pre>
        </fieldset>

    </section>

    <section id="code">

        <h2>Code</h2>

        <p>
            To start the project, create a file named <code>Program.cs</code> in an empty directory, and copy in the code
            below.
        </p>

        <fieldset>
            <legend>Example Program file</legend>
            <pre><code class="language-csharp">// setup XmppClient with some properties
var xmppClient = new XmppClient(
        conf =>
        {
            conf
                .UseSocketTransport(new StaticNameResolver(new Uri("tcp://localhost:5222")))
                .WithCertificateValidator(new AlwaysAcceptCertificateValidator());
        }
    )
    {
        Jid = "john@example.org",
        Password = "secret"
    };

// subscribe to the Binded session state
xmppClient
    .StateChanged
    .Where(s => s == SessionState.Binded)
    .Subscribe(async v =>
    {
        // request roster (contact list).
        // This is optional, but most chat clients do this on startup
        var roster = await xmppClient.RequestRosterAsync();

        // send our online presence to the server
        await xmppClient.SendPresenceAsync(Show.Chat, "free for chat");

        // send a chat message to user2
        await xmppClient.SendChatMessageAsync("jane@example.org", "This is a test");
    });

// connect so the server
await xmppClient.ConnectAsync();

// wait for a key press
Console.ReadLine();

// Close connection again
await xmppClient.DisconnectAsync();</code></pre>
        </fieldset>

        <p>
            Use any other XMPP client to log in with the user 'jane', then run this code. You will see that it will send
            a message to Jane!
        </p>

        <p>
            Note that this example disables important security features. You should not use this for anything important!
        </p>

    </section>

    <section id="references">

        <h2>Further Reading</h2>

        <p>
            Please use the links below to find more information.
        </p>
        <ul>
            <li><a href="https://xmppdotnet.org/">XmppDotNet project home page</a></li>
            <li><a href="https://xmppdotnet.org/docs/get-started/">XmppDotNet "get started" documentation</a></li>
        </ul>
    </section>

    <footer>
        <p>
            An active support community for Openfire is available at
            <a href="https://discourse.igniterealtime.org">https://discourse.igniterealtime.org</a>.
        </p>
    </footer>

</article>

</body>
</html>
